package com.project.light.service.impl;

import com.project.light.entity.System;
import com.project.light.entity.Users;
import com.project.light.mapper.SystemMapper;
import com.project.light.mapper.UsersMapper;
import com.project.light.service.LoginService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private SystemMapper systemMapper;
    @Resource
    private UsersMapper usersMapper;

    @Override
    public System getBySys(String sysId, String sysName) {
        Example example = new Example(System.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("sysId", sysId);
        c.andEqualTo("sysName", sysName);
        c.andEqualTo("deleteFlag", 0);
        List<System> list = systemMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public Users getUserBySys(String sysName) {
        Example example = new Example(Users.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("userName", sysName);
        c.andEqualTo("deleteFlag", 0);
        List<Users> list = usersMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }

    public System getByToken(String token) {
        Example example = new Example(System.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("token", token);
        c.andEqualTo("deleteFlag", 0);
        List<System> list = systemMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystem(System entity) throws Exception {
        systemMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystem(System entity) throws Exception {
        systemMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertUsers(Users entity) throws Exception {
        usersMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateUsers(Users entity) throws Exception {
        usersMapper.updateByPrimaryKeySelective(entity);
    }
}
